<!-- 在这个文件对每个tab对应的列表进行渲染 -->
<template>
	<view class="paryorder-content">
		<!-- 这里设置了z-paging加载时禁止自动调用reload方法，自行控制何时reload（懒加载）-->
		<!--  :enable-back-to-top="currentIndex===tabIndex" 在微信小程序上可以多加这一句，因为默认是允许点击返回顶部的，但是这个页面有多个scroll-view，会全部返回顶部，所以需要控制是当前index才允许点击返回顶部 -->
		<z-paging ref="paging" v-model="dataList" @query="queryList" :fixed="false" :auto="true">
			<template #top>
				<view class="" style="background-color: #F8F8F8;">
					<u-navbar :border-bottom="false" title="我的级别" :background="{'background':'#F8F8F8'}" :title-size="36" :title-bold="true" :title-color="textColor">
						<template #right>
							<view class="u-p-r-20"  @click="editTemplateTapFn('add')">
								新增
							</view>
						</template>
						
					</u-navbar>
					<view class="u-p-20" style="background-color: #F8F8F8;">
						<u-search :searchIconFlag="true" v-model="searchText" input-align="center" placeholder="级别名称" height="70" :border-color="'#FFBB1A'" :bg-color="'#F8F8F8'" 	:show-action="false" @change="reloadFn" @search="reloadFn"></u-search>
					</view>
				</view>
			</template>
			<!-- 如果希望其他view跟着页面滚动，可以放在z-paging标签内 -->
			<view class="payorder-container" v-for="(item,index) in dataList" :key="index" >
				<view class="payorder-section">
						<view class="u-flex u-row-center u-m-r-20" style="width: 80rpx;height: 80rpx;border-radius: 50%;background-color: #f8f8f8;">
							<text class="u-font-44" style="font-weight: bold;">{{item.levelValue}}</text>
						</view>
						<view class="section-list ">
						<view class="section-item u-m-l-18">
							<view class="section-item-name u-line-1" style="width:100%;">
							{{item.name}}
							<u-icon name="edit-pen" class="u-m-l-14" @click="editTemplateTapFn('edit',item.name,item.id,item.levelValue)"></u-icon>
							</view>
							<view class="section-item-count">已设置  {{item.itemCount}}  个商品</view>
						</view>
					
					</view>
					<view class="u-flex u-col-center u-row-between u-m-l-20" style="height: 100%;">
						<view class="section-btn-box u-m-r-40" @click="$mRouter.push({ route: '/pages/owners/levelMange/levelItemMange?templateId='+item.id+'&name='+item.name })">
							<image src="/static/owners/price.png" mode="" style="height: 46rpx;width: 46rpx;"></image>
							<text class="btn-text">管理价格</text>
						</view>
						
						<view class="section-btn-box" @click="exportPriceBtnTapFn()">
							<image src="/static/owners/exportPrice.png" mode="" style="height: 46rpx;width: 46rpx;" ></image>
							<text class="btn-text">价格导入</text>
						</view>
					</view>
				</view>
			</view>
		</z-paging>
		<u-modal v-model="delModel" :content="'您确定要移除此级别吗？'"  @confirm="delTemplateFn()" ></u-modal>
		<u-modal v-model="editTemplate"  @confirm="editTemplateFn()" :title="editType==='add'?'新增级别':'修改级别名称'">
			<view class="u-p-35 " >
				<view class="u-flex">
					<text>名称：</text>
					<u-input v-model="templateName" border placeholder="请输入要生成的级别名称" type="text" />
				</view>
				<view class="u-flex u-m-t-22" @click="levelValueShow = true">
					<text>级别：</text>
					<view class="u-flex  u-row-between u-p-t-20 u-p-b-20 u-p-20" style="flex: 1; border: 1px solid #eaeaea;">
						<text>{{chooseLevelValue}}</text>
						<u-icon name="arrow-right"></u-icon>
					</view>
				
				</view>
			</view>
		</u-modal>
		<u-select v-model="levelValueShow" :list="levelValueList" @confirm="selectConfirm"></u-select>
		
		<u-modal v-model="chooseLevelModel"  @confirm="exportTemplateFn()" @cancel="camcelLevelModel" title="从其他级别中导入价格">
			<view class="u-p-35 " >
				<view  class=" u-p-t-30 u-p-b-30 u-font-32  u-flex u-col-center" style="background-color: #fff;">
					<view class="">
						级别
					</view>
					<view class="u-flex u-col-center u-row-between"  style="flex:1;" @click="templateShow = true">
						<view class="u-m-l-30"  :style="{'flex':1,'color':chooseLevelName==='请选择需要引入的级别'?'rgb(192, 196, 204)':'#333'}">
							{{chooseLevelName}}
						</view>
						<u-icon name="arrow-right"></u-icon>
					</view>
					
				</view>
				<view  class=" u-p-t-30 u-p-b-30 u-font-32  u-flex u-col-center" style="background-color: #fff;">
					<view class="u-m-r-20">
						方式
					</view>
					<view class="u-flex u-col-center u-row-between"  style="flex:1;" >
						<u-radio-group v-model="exportMethod" active-color='#ffbb1a'>
							<u-radio :name="1" >新增</u-radio>
							<u-radio :name="2" >覆盖</u-radio>
							<u-radio :name="3" >新增且覆盖</u-radio>
						</u-radio-group>
					</view>
					
				</view>
			</view>
		</u-modal>
		
		<u-popup v-model="templateShow" mode="bottom">
			<view style="min-height: 800rpx;">
				<view class="u-text-center  u-font-32" style="line-height: 100rpx;height: 100rpx;font-weight: bold;">
					选择需要引用的级别
				</view>
				<levelList :chooseId="chooseLevelId" @sendValue="sendValue"></levelList>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import levelList from './compontents/levelList.vue';
	import { useShopStore } from '@/store/shop.js';
	import { mapState } from 'pinia';
	export default {
		components:{levelList},
		data() {
			return {
				//v-model绑定的这个变量不要在分页请求结束中自己赋值！！！
				dataList: [],
				searchText:'',
				editTemplate:false,
				delModel:false,
				templateName:'',
				chooseTemplateId:'',
				chooseLevelValue:0,
				editType:'',
				levelValueShow:false,
				levelValueList:[],
				chooseLevelModel:false,
				templateShow:false,
				chooseLevelName:'请选择需要引入的级别',
				exportMethod:1,
				chooseLevelId:'',
				destinationShopLevelId:'',
				
			}
		},
		created(){
			// this.$refs.paging.reload();
			for(let i = 0;i<20;i++){
				this.levelValueList.push({
					label:i,
					value:i
				})
			};
		},
		computed:{
			...mapState(useShopStore, {storeShop: 'shop'})	
		},
		methods: {
			reloadFn(){
				this.$refs.paging.reload();
			},
			selectConfirm(val){
				this.chooseLevelValue = val[0].value;
			},
			exportPriceBtnTapFn(id){
				this.chooseLevelModel = true;
				this.destinationShopLevelId = id;
			},
			sendValue(val){
				this.chooseLevelId = val.id;
				this.chooseLevelName = val.name;
				this.templateShow = false;
			},
			camcelLevelModel(){
				this.chooseLevelId = '';
				this.chooseLevelName = '请选择需要引入的级别';
			},
			// 编辑级别名称弹窗开启
			editTemplateTapFn(type,name,id,value){
				if(type === 'add'){
					this.chooseTemplateId = '';
					this.templateName = '';
					this.chooseLevelValue = 0;
				}else{
					this.chooseTemplateId = id;
					this.templateName = name;
					this.chooseLevelValue = value;
				}
				this.editType = type;
				this.editTemplate = true;
			},
			async editTemplateFn(){
				uni.showLoading({
					title:"loading..."
				});
				let params = {
					name:this.templateName,
					levelValue:this.chooseLevelValue
				}
				if(this.editType ==='add'&&this.templateName === ''){
					uni.hideLoading();
					this.$u.toast("级别名称不得为空!");
					return false;
				}
				if(this.editType === 'edit'){
					params.id = this.chooseTemplateId;
				}
				const res = await this.$newHttp.post('api/BrandOwner/SaveShopLevel',params);
				uni.hideLoading();
				if(res.code === 200){
					uni.showToast({
						title:this.editType === 'edit'?'修改成功':'新增成功',
					});
					this.$refs.paging.reload();
				}else{
					this.$u.toast(this.editType === 'edit'?'修改失败':'新增失败');
				}
			},
			// 从其他级别中导入价格
			async exportTemplateFn(){
				uni.showLoading({
					title:"loading..."
				})
				if(this.chooseLevelId === ''){
					uni.hideLoading();
					this.$u.toast('导入级别不得为空');
					return false;
				};
				let params = {
					shopId:this.storeShop.id,
					sourceShopLevelId:this.chooseLevelId,
					copyPolicy:this.exportMethod,
					destinationShopLevelId:this.destinationShopLevelId
				}
				const res =  await this.$newHttp.post('api/BrandOwner/CopyPrices',params);
				uni.hideLoading();
				if(res.code === 200){
					uni.showToast({
						title:'级别价格导入成功',
					})
					this.templateName = '';
					this.chooseLevelId = '';
					this.chooseLevelName= '';
					this.exportMethod = 1;
					this.chooseLevelModel = false;
				}else{
					this.$u.toast('级别价格导入失败');
				}
			},
			// 删除按钮点击
			delTemplateTap(id){
				this.chooseTemplateId = id;
				this.delModel = true;
			},
			// 删除价格级别
			async delTemplateFn(){
				uni.showLoading({
					title:"loading..."
				})
				let params = {
					id:this.chooseTemplateId,
					shopId:this.storeShop.id,
					isDeleted:false,
					
				}
				const res = await this.$newHttp.delete('api/BrandOwner/DeleteShopLevel',params);
				console.log(res);
				uni.hideLoading();
				if(res.code === 200){
					uni.showToast({
						title:'删除成功',
					});
					this.$refs.paging.reload();
				}else{
					this.$u.toast('删除失败');
				}
			},
			async queryList(pageNo, pageSize) {
				//组件加载时会自动触发此方法，因此默认页面加载时会自动触发，无需手动调用
				//这里的pageNo和pageSize会自动计算好，直接传给服务器即可
				//模拟请求服务器获取分页数据，请替换成自己的网络请求
				const params = {
					pageIndex: pageNo,
					pageSize: pageSize,
					shopId:this.storeShop.id,
					isDeleted:false
				}
				if(this.searchText!==''){
					params.name = this.searchText;
				}
				uni.showLoading({
					title: '加载中...',
					mask: false
				});
				const res = await this.$newHttp.post('api/BrandOwner/GetShopLevelsByPage', params );
				uni.hideLoading({
					title: '加载中...'
				})
				if(res.code === 200){
					this.$refs.paging.complete(res.data.list);
				}else{
					this.$refs.paging.complete(false);
				}
			},
			itemClick(item) {
				console.log('点击了', item.title);
				this.$mRouter.push({ 
					route: '/pages/sale/payorder/createPayOrder?shop='+item.id,
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	/* 注意:父节点需要固定高度，z-paging的height:100%才会生效 */
	.paryorder-content {
		height: 100%;
	}
	.payorder-container{
		background-color: #fff;
		box-sizing: border-box;
		padding: 0 30rpx;
		.payorder-header{
			display: flex;
			justify-content: space-between;
		}
		.payorder-section{
			display: flex;
			align-items: center;
			box-sizing: border-box;
			padding: 35rpx 0;
			margin-bottom: 20rpx;
			.section-image{
				width: 80rpx;
				height: 80rpx;
				display: block;
				border-radius: 50%;
				margin-right: 32rpx;
			}
			.section-list{
				flex: 1;
				overflow: hidden;
				// margin-left: 20rpx;
				.section-btn{
					width: 160rpx;
					margin-left: 10rpx;
					height:70rpx;
					border-radius: 35rpx;
					border: 1rpx solid #FFBB1A;
					color: #FFBB1A;
					text-align: center;
					line-height: 70rpx;
				}
				.section-btn-disabled{
					border-color:#999999;
					color: #999999;
				}
				.section-item{
					width: 100%;
					.section-item-name{
						font-size: 32rpx;
						font-weight: 500;
						margin-bottom: 20rpx;
					}
					.section-item-count{
						font-size: 24rpx;
						color: #999;
					}
					
				}
			}
			.section-btn-box{
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				>.btn-text{
					font-size: 20rpx;
					margin-top: 10rpx;
				}
			}
			
		}
		
	}


</style>
